package com.imooc.service.impl;

import com.imooc.idworker.Sid;
import com.imooc.mapper.UsersFansMapper;
import com.imooc.mapper.UsersLikeVideosMapper;
import com.imooc.mapper.UsersMapper;
import com.imooc.mapper.UsersReportMapper;
import com.imooc.pojo.*;
import com.imooc.service.UserService;
import com.imooc.utils.IMoocJSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @author Zhou
 * @create 2020-11-14 9:22
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private UsersLikeVideosMapper usersLikeVideosMapper;

    @Autowired
    private UsersFansMapper usersFansMapper;

    @Autowired
    private UsersReportMapper usersReportMapper;

    @Autowired
    private Sid sid;

    @Override
    public boolean queryUsernameIsExit(String username) {
        UsersExample usersExample = new UsersExample();
        UsersExample.Criteria criteria = usersExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<Users> users = usersMapper.selectByExample(usersExample);
        return !users.isEmpty();
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int saveUser(Users user) {
        String userId = sid.nextShort();
        user.setId(userId);
        return usersMapper.insert(user);
    }

    @Override
    public Users queryUserByUsername(String username, String password) {
        UsersExample usersExample = new UsersExample();
        UsersExample.Criteria criteria = usersExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andPasswordEqualTo(password);
        List<Users> users = usersMapper.selectByExample(usersExample);
        return users.isEmpty() ? null : users.get(0);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateUserInfo(Users user) {
        UsersExample usersExample = new UsersExample();
        UsersExample.Criteria criteria = usersExample.createCriteria();
        criteria.andIdEqualTo(user.getId());
        usersMapper.updateByExampleSelective(user, usersExample);
    }

    @Override
    public Users queryUserById(String userId) {
        UsersExample usersExample = new UsersExample();
        UsersExample.Criteria criteria = usersExample.createCriteria();
        criteria.andIdEqualTo(userId);
        List<Users> users = usersMapper.selectByExample(usersExample);
        return users.isEmpty() ? null : users.get(0);
    }

    @Override
    public boolean isUserLikeVideo(String userId, String videoId) {
        if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(videoId)){
            return false;
        }

        UsersLikeVideosExample example = new UsersLikeVideosExample();
        UsersLikeVideosExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andVideoIdEqualTo(videoId);
        List<UsersLikeVideos> usersLikeVideos = usersLikeVideosMapper.selectByExample(example);
        if (usersLikeVideos != null && usersLikeVideos.size() > 0) {
            return true;
        }
        return false;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void saveUserFanRelation(String userId, String fanId) {
        UsersFans usersFans = new UsersFans();
        String id = sid.nextShort();
        usersFans.setId(id);
        usersFans.setUserId(userId);
        usersFans.setFanId(fanId);
        usersFansMapper.insert(usersFans);

        usersMapper.addFansCount(userId);
        usersMapper.addFollowerCount(fanId);
    }


    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteUserFanRelation(String userId, String fanId) {
        UsersFansExample example = new UsersFansExample();
        UsersFansExample.Criteria criteria = example.createCriteria();
        criteria.andFanIdEqualTo(fanId);
        criteria.andUserIdEqualTo(userId);

        usersFansMapper.deleteByExample(example);

        usersMapper.deduceFansCount(userId);
        usersMapper.deduceFollowerCount(fanId);
    }

    @Override
    public boolean queryIfFollow(String userId, String fanId) {
        UsersFansExample example = new UsersFansExample();
        UsersFansExample.Criteria criteria = example.createCriteria();
        criteria.andFanIdEqualTo(fanId);
        criteria.andUserIdEqualTo(userId);
        List<UsersFans> usersFans = usersFansMapper.selectByExample(example);

        if (usersFans != null && usersFans.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public void reportUser(UsersReport usersReport) {
        String userId = sid.nextShort();
        usersReport.setId(userId);
        usersReport.setCreateDate(new Date());

        usersReportMapper.insert(usersReport);
    }
}
